package cn.yzgy.yzgyoa.finance.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.EnableDict;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.finance.dto.CapitalFinanceRecordDto;
import cn.yzgy.yzgyoa.finance.entity.CapitalFinanceRecord;
import cn.yzgy.yzgyoa.finance.service.ICapitalFinanceRecordService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import cn.yzgy.yzgyoa.common.controller.BaseController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2021-07-14
 */
@Slf4j
@Syslog
@RestController
@RequestMapping("/finance/capital-finance-record")
public class CapitalFinanceRecordController extends BaseController {

    private ICapitalFinanceRecordService capitalFinanceRecordService;

    public CapitalFinanceRecordController(ICapitalFinanceRecordService capitalFinanceRecordService) {
        this.capitalFinanceRecordService = capitalFinanceRecordService;
    }

    @ApiDesc("分页获取收支列表")
    @EnableDict
    @GetMapping("getAllCapitalDetail")
    @RequiresPermissions("api:capital:capitalList:getAllCapitalDetail")
    public Urmf getAllCapitalDetail(@RequestParam Integer page, @RequestParam Integer size, CapitalFinanceRecordDto queryDto) {
        //收支统计调用时不进行分页
        if(queryDto.getRequestSource()!=null){
            page=1;
            size=-1;
        }
        //随便天打印一些
        IPage<CapitalFinanceRecordDto> staffPage = capitalFinanceRecordService.getAllCapitalDetail(new Page<>(page, size), queryDto);
        return Urmf.success(getCurrentMethodName(), staffPage);
    }

    @GetMapping("/exportCapitalRecord")
    @ApiDesc("导出收支记录统计汇总报表")
    @RequiresPermissions("api:capital:capitalList:exportCapitalRecord")
    public Urmf exportCapitalRecord(CapitalFinanceRecordDto queryDto) {
        String id = capitalFinanceRecordService.exportCapitalRecord(queryDto, getCurrentUser());
        return Urmf.success(getCurrentMethodName(),id);
    }

    @ApiDesc("新增收支记录")
    @EnableDict
    @PostMapping("capitalAdd")
    @RequiresPermissions("api:capital:capitalList:capitalAdd")
    public Urmf capitalAdd(@RequestBody CapitalFinanceRecord queryDto) {
        boolean insert = capitalFinanceRecordService.capitalAdd(getCurrentUser(),queryDto);
        if(insert){
            return Urmf.success(getCurrentMethodName(), queryDto);
        }
        return Urmf.fail(getCurrentMethodName(), queryDto);
    }

    @ApiDesc("修改收支记录")
    @EnableDict
    @PutMapping("capitalEdit")
    @RequiresPermissions("api:capital:capitalList:capitalEdit")
    public Urmf capitalEdit(@RequestBody CapitalFinanceRecord queryDto) {
        boolean insert = capitalFinanceRecordService.capitalEdit(getCurrentUser(),queryDto);
        if(insert){
            return Urmf.success(getCurrentMethodName(), queryDto);
        }
        return Urmf.fail(getCurrentMethodName(), queryDto);
    }

    @ApiDesc("删除收支记录")
    @DeleteMapping("capitalDel")
    @RequiresPermissions("api:capital:capitalList:capitalDel")
    public Urmf capitalDel(@RequestBody CapitalFinanceRecordDto queryDto) {
        boolean del = capitalFinanceRecordService.capitalDel(queryDto.getDelId());
        if(del){
            return Urmf.success(getCurrentMethodName(), "删除数据成功！");
        }
        return Urmf.fail(getCurrentMethodName(), "删除数据失败！");
    }

}
